package com.easymin.weixin.common;  
  
import java.io.UnsupportedEncodingException;  
import java.security.InvalidAlgorithmParameterException;  
import java.security.InvalidKeyException;  
import java.security.NoSuchAlgorithmException;  
import javax.crypto.BadPaddingException;  
import javax.crypto.Cipher;  
import javax.crypto.IllegalBlockSizeException;  
import javax.crypto.NoSuchPaddingException;  
import javax.crypto.spec.IvParameterSpec;  
import javax.crypto.spec.SecretKeySpec;  

  
public class AesUtil {  
    public static final String VIPARA = "RWA7XBFXQBA2S00E";  
    public static final String bm = "utf-8";  
  
  
  
 
    /** 
     * AES 加密 
     *  
     * @param content 
     *            明文 
     * @param password 
     *            生成秘钥的关键字 
     * @return 
     */  
  
    public static String aesEncrypt(String content, String password) {  
        try {  
            IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());  
            SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");  
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");  
            cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);  
            byte[] encryptedData = cipher.doFinal(content.getBytes(bm));  
              
            return Base64.encode(encryptedData);  
//          return byte2HexStr(encryptedData);  
        } catch (NoSuchAlgorithmException e) {  
            e.printStackTrace();  
        } catch (NoSuchPaddingException e) {  
            e.printStackTrace();  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        } catch (InvalidKeyException e) {  
            e.printStackTrace();  
        } catch (IllegalBlockSizeException e) {  
            e.printStackTrace();  
        } catch (BadPaddingException e) {  
            e.printStackTrace();  
        } catch (InvalidAlgorithmParameterException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
  
        return null;  
    }  
  
    /** 
     * AES 解密 
     *  
     * @param content 
     *            密文 
     * @param password 
     *            生成秘钥的关键字 
     * @return 
     */  
  
    public static String aesDecrypt(String content, String password) {  
        try {  
            byte[] byteMi = Base64.decode(content);  
//          byte[] byteMi=  str2ByteArray(content);  
            IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());  
            SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");  
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");  
            cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);  
            byte[] decryptedData = cipher.doFinal(byteMi);  
            return new String(decryptedData, "utf-8");  
        } catch (NoSuchAlgorithmException e) {  
            e.printStackTrace();  
        } catch (NoSuchPaddingException e) {  
            e.printStackTrace();  
        } catch (InvalidKeyException e) {  
            e.printStackTrace();  
        } catch (IllegalBlockSizeException e) {  
            e.printStackTrace();  
        } catch (BadPaddingException e) {  
            e.printStackTrace();  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        } catch (InvalidAlgorithmParameterException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        return null;  
    }  
    
}  